/* eslint-disable */
import { createApp } from 'vue'
import Vue from 'vue'

import ElementPlus, { ElStep } from 'element-plus'
import 'element-plus/dist/index.css'

import 'amfe-flexible'
import '@/assets/css/common.css'
import '@/assets/css/element-theme.css'

import '@/assets/css/tabs.css';
import router from './router'
import App from './App.vue'


import ViewUIPlus from 'view-ui-plus'
import 'view-ui-plus/dist/styles/viewuiplus.css'
// vuex状态管理 start-------------------------------- 
import store from '@/store/index.js';

// 全局错误处理 start--------------------------------
import { initGlobalErrorHandler } from '@/utils/errorHandler.js'
// 初始化全局错误处理
initGlobalErrorHandler()
// 全局错误处理 end--------------------------------
// new Vue({
//     store,
//     render: h => h(App)
// }).$mount('#app')
// vuex end-------------------------------- 




const app = createApp(App);
app.use(ElementPlus).use(router).use(store).use(ViewUIPlus).mount('#app')



// echarts start-------------------------------- 
// import * as echarts from 'echarts'
// vue3 给原型上挂载属性

// echarts end-------------------------------- 


// axios start--------------------------------
import axios from "axios";
app.config.globalProperties.$axios = axios
axios.defaults.withCredentials = true;
// createApp.prototype.axios = axios;
import { ElLoading, ElMessage } from 'element-plus'
var loadingInstance;
var configurl = null
axios.interceptors.request.use(
    config => {
        loadingInstance = ElLoading.service()
        // loadingInstance = ElLoading.service({ target: 'div#loading_box' })
        let token = localStorage.getItem('token');
        // 如果存在 token,在请求头中携带
        if (token) {
            config.headers.Authorization = token;
        }
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);
axios.interceptors.response.use(
    res => {
        if (loadingInstance) {
            loadingInstance.close();
        }
        let data = res.data
        if (data.code == 0) {
            configurl = null
            return data
        } else if (data.code == 401) {
            // 只有401未授权错误才显示登录提示并跳转
            ElMessage.error(data.message || '请先登录')
            if (data.loginCode == 0) {
                setTimeout(() => {
                    router.push({ path: '/loginPage' })
                }, 700);
            }
            return data
        } else {
            // 其他业务错误不显示错误消息，让页面自己处理
            // 只有明确需要登录的接口才显示登录提示
            if (data.loginCode == 0) {
                ElMessage.error(data.message || '请先登录')
                setTimeout(() => {
                    router.push({ path: '/loginPage' })
                }, 700);
            }
            // 对于首页等公开页面，不显示错误消息，让页面自己处理空状态
            return data
        }
    },
    error => {
        console.log("error----------------------------");
        console.log(error)
        if (loadingInstance) {
            loadingInstance.close();
        }
        // Try to surface a meaningful error message to the user
        let message = '请求失败，请稍后重试';
        if (error && error.response) {
            const response = error.response;
            const data = response.data;
            if (data && typeof data === 'object') {
                message = data.message || data.msg || message;
            } else if (typeof data === 'string') {
                message = data;
            } else if (response.statusText) {
                message = response.statusText;
            }
        } else if (error && error.message) {
            message = error.message;
        }
        ElMessage.error(message);
        return Promise.reject(error);
    }
);
//axios end------------------------------------
// commonjs
//
// import commonJs from "@/assets/js/common.js"
// app.config.globalProperties.$commonJs = commonJs
//
//
// commonJs.getTahaoSelectF().then((res) => {
//     app.config.globalProperties.$tahao_select_com = res;
// });
//

//时间戳转时间格式
app.config.globalProperties.$formateDateTime = (shijianchuo, type) => {
    if (!shijianchuo) {
        shijianchuo = Date.parse(new Date());
    }
    shijianchuo = parseInt(shijianchuo);
    var date = new Date(shijianchuo);
    var yyyy = date.getFullYear(); //获取完整的年份(4位,1970)
    var m = date.getMonth() + 1; //获取月份(0-11,0代表1月,用的时候记得加上1)
    if (m.toString().length == 1) {
        m = "0" + m;
    }
    var d = date.getDate(); //获取日(1-31)
    if (d.toString().length == 1) {
        d = "0" + d;
    }
    var h = date.getHours(); //获取小时数(0-23)
    if (h.toString().length == 1) {
        h = "0" + h;
    }
    var mm = date.getMinutes(); //获取分钟数(0-59)
    if (mm.toString().length == 1) {
        mm = "0" + mm;
    }
    var s = date.getSeconds(); //获取秒数(0-59)
    if (s.toString().length == 1) {
        s = "0" + s;
    }
    var datetime = "";
    if (!type) {
        datetime = yyyy + "-" + m + "-" + d;
    }
    if (type == 1) {
        datetime = yyyy + "-" + m + "-" + d + " " + h + ":" + mm + ":" + s;
    }
    return datetime;
};



//组织机构下拉列表
import { orgSelectApi } from '@/assets/api/api.js';
app.config.globalProperties.$getOrgSelect = (cb) => {
    orgSelectApi()
        .then(res => {
            cb(res.data)
        })
        .catch(err => { });
};
//用户类型下拉列表
import { userTypeSelectApi } from '@/assets/api/api.js';
app.config.globalProperties.$getUserTypeSelect = (cb) => {
    userTypeSelectApi()
        .then(res => {
            cb(res.data)
        })
        .catch(err => { });
};
//用户角色下拉列表
import { roleSelectApi } from '@/assets/api/api.js';
app.config.globalProperties.$getRoleSelect = (cb) => {
    roleSelectApi()
        .then(res => {
            cb(res.data)
        })
        .catch(err => { });
};
//外包项目选项下拉列表
import { orderItemSelectApi } from '@/assets/api/api.js';
app.config.globalProperties.$getOrderItemSelect = (cb) => {
    orderItemSelectApi()
        .then(res => {
            cb(res.data)
        })
        .catch(err => { });
};
//外包项目结算单位下拉列表
import { orderItemUnitSelectApi } from '@/assets/api/api.js';
app.config.globalProperties.$getOrderItemUnitSelect = (cb) => {
    orderItemUnitSelectApi()
        .then(res => {
            cb(res.data)
        })
        .catch(err => { });
};
//业务类型下拉列表
import { orderItemTypeSelectApi } from '@/assets/api/api.js';
app.config.globalProperties.$getOrderItemTypeSelect = (cb) => {
    orderItemTypeSelectApi()
        .then(res => {
            cb(res.data)
        })
        .catch(err => { });
};
//工单状态下拉列表
import { orderStatusSelectApi } from '@/assets/api/api.js';
app.config.globalProperties.$getOrderStatusSelect = (cb) => {
    orderStatusSelectApi()
        .then(res => {
            cb(res.data)
        })
        .catch(err => { });
};
//分公司 下拉列表
import { companySelectApi } from '@/assets/api/api.js';
app.config.globalProperties.$getCompanySelect = (cb) => {
    companySelectApi()
        .then(res => {
            cb(res.data)
        })
        .catch(err => { });
};












